import { defineStore } from 'pinia'
import { constantRoutes, dynamicRoutes } from '../../router/index'

const hasPermission = (menus, route)=>{
    if(!route.name){
        return true
    }
    const routeMenu = findRouteMenu(menus, route.name)
    if(routeMenu!=null){
        //设置菜单的标题、图标和可见性
        if(routeMenu.title!=null && routeMenu.title!==''){
            if(route.meta==undefined || route.meta==null){
                route.meta = {}
            }
            route.meta.title = routeMenu.title
        }
        if(routeMenu.icon!=null && routeMenu.title!==''){
            if(route.meta==undefined || route.meta==null){
                route.meta = {}
            }
            route.meta.icon = routeMenu.icon
        }
        if(routeMenu.hidden!=null){
            if(route.meta==undefined || route.meta==null){
                route.meta = {}
            }
            route.meta.hidden = routeMenu.hidden!==0
        }
        if(routeMenu.sort!=null){
            route.sort = routeMenu.sort
        }
        return true
    } else {
       if(route.hidden===true){
        route.sort = 9999 
        return true
       } else {
         return false
       }
    }
}

const findRouteMenu = (menus, name)=>{
    if(!menus){
        return null
    }
    for(let i=0;i<menus.length;i++){
        if(name==menus[i].name){
            return menus[i]
        }
    }
    return null
}

const sortRoutes = (accessRoutes)=>{
   for(let i=0;i<accessRoutes.length;i++){
      if(accessRoutes[i].children && accessRoutes[i].children.length>1){
        sortRoutes(accessRoutes[i].children)
      }
   }
   accessRoutes.sort((item1,item2)=>{
      return item1.sort - item2.sort
   })

}

const usePermissionStore = defineStore({
    id: 'permission',
    state: ()=>({
        constantRoutes: constantRoutes,
        dynamicRoutes: [],
        routes: constantRoutes
    }),
    getters: {
        constantRouteList: (state)=>state.constantRoutes,
        dynamicRouteList: (state)=>state.dynamicRoutes,
        routeList: (state)=>state.routes
    },
    actions: {
        setDynamicRoutes(dynamicRoutes){
           this.dynamicRoutes = dynamicRoutes
           this.routes = this.constantRoutes.concat(dynamicRoutes)
        },
        genDynamicRoutes(menus){
            return new Promise(resolve=>{
               const accessRoutes = dynamicRoutes.filter(route=>{
                   if(hasPermission(menus, route)){
                       if(route.children && route.children.length>0){
                          route.children = route.children.filter(child=>{
                              if(hasPermission(menus, child)){
                                 return true
                              } else {
                                 return false
                              }
                          })
                       } else {
                         return true
                       }
                   } else {
                      return false
                   }
               })
               sortRoutes(dynamicRoutes)
               this.setDynamicRoutes(dynamicRoutes)
               resolve(dynamicRoutes)
            })
        }
    }
})

export default usePermissionStore